#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <inttypes.h>
#include <time.h>
#include <genrand.h>
#include <hexstr.h>
// include cmd id function
#include <ltCmdList.h>
#include <paramNormal.h>

#define checkLtCmdAddr 0x01

int chkSubParseRcv(void *inptr, uint8_t *data, int dsize)
{
    for (int i = 0; i < dsize; i++)
    {
        if (ltPtcSubPsAndDeal(inptr, data[i]) <= 0)
        {
            continue;
        }
        return 1;
    }
    return -1;
}
int chkHostParseRcv(void *inptr, uint8_t *data, int dsize)
{
    for (int i = 0; i < dsize; i++)
    {
        if (ltPtcHostPsAndDeal(inptr, data[i]) <= 0)
        {
            continue;
        }
        return 1;
    }
    return -1;
}
void chkHostRwSn(void *hostptr, void *subptr)
{
    int tmpflag = 0;
    ltCmdStcSnd_rwSn tmpdata;
    grdStrNorm(tmpdata.wSn, sizeof(tmpdata.wSn));
    if (ltCmdFuncHostPack_rwSn(ltPtcStcPtr(hostptr), 0xff, 0, &tmpdata) < 0)
    {
        logerror("");
    }
    logHexBufStr("chkHostRwSn", ltPtcGetPackPtr(hostptr), ltPtcGetPackSizeV1(hostptr));
    tmpflag = chkSubParseRcv(subptr, ltPtcGetPackPtr(hostptr), ltPtcGetPackSizeV1(hostptr));
    if (tmpflag == 0)
    {
        logdebug("sub do wait:");
    }
    else if (tmpflag < 0)
    {
        logdebug("sub do error:");
    }
    tmpflag = chkHostParseRcv(hostptr, ltPtcGetPackPtr(subptr), ltPtcGetPackSizeV1(subptr));
    if (tmpflag == 0)
    {
        logdebug("host do wait:");
    }
    else if (tmpflag < 0)
    {
        logdebug("host do error:");
    }
    logdebug("%.*s", LT_CMD_SN_MAX_SIZE, parmNormIstc.sn);
}
void chkHostRwAddr(void *hostptr, void *subptr)
{
    int tmpflag = 0;
    ltCmdStcSnd_rwAddr tmpdata;
    memcpy(tmpdata.cfgSn, parmNormIstc.sn, sizeof(parmNormIstc.sn));
    tmpdata.wAddr = checkLtCmdAddr;
    tmpdata.rwflag = 1;
    if (ltCmdFuncHostPack_rwAddr(ltPtcStcPtr(hostptr), 0xff, 0, &tmpdata) < 0)
    {
        logerror("");
    }
    logHexBufStr("chkHostRwAddr", ltPtcGetPackPtr(hostptr), ltPtcGetPackSizeV1(hostptr));
    tmpflag = chkSubParseRcv(subptr, ltPtcGetPackPtr(hostptr), ltPtcGetPackSizeV1(hostptr));
    logdebug("%d", tmpflag);
    if (tmpflag == 0)
    {
        logdebug("sub do wait:");
    }
    else if (tmpflag < 0)
    {
        logdebug("sub do error:");
    }
    tmpflag = chkHostParseRcv(hostptr, ltPtcGetPackPtr(subptr), ltPtcGetPackSizeV1(subptr));
    if (tmpflag == 0)
    {
        logdebug("host do wait:");
    }
    else if (tmpflag < 0)
    {
        logdebug("host do error:");
    }
    logdebug("%d", parmNormIstc.addr485);
}
void chkHostGetInfo(void *hostptr, void *subptr)
{
    int tmpflag = 0;
    ltCmdStcSnd_getInf tmpdata;
    grdStrNorm(tmpdata.dlgKey, 16);
    if (ltCmdFuncHostPack_getInf(ltPtcStcPtr(hostptr), parmNormIstc.addr485, 0, &tmpdata) < 0)
    {
        logerror("");
    }
    logHexBufStr("chkHostGetInfo pack", ltPtcGetPackPtr(hostptr), ltPtcGetPackSizeV1(hostptr));
    tmpflag = chkSubParseRcv(subptr, ltPtcGetPackPtr(hostptr), ltPtcGetPackSizeV1(hostptr));
    logdebug("%d", tmpflag);
    if (tmpflag == 0)
    {
        logdebug("sub do wait:");
    }
    else if (tmpflag < 0)
    {
        logdebug("sub do error:");
    }
    tmpflag = chkHostParseRcv(hostptr, ltPtcGetPackPtr(subptr), ltPtcGetPackSizeV1(subptr));
    if (tmpflag == 0)
    {
        logdebug("host do wait:");
    }
    else if (tmpflag < 0)
    {
        logdebug("host do error:");
    }
#if LT_PTC_CMD_USE_CRYPTE
    logHexBufStr("chkHostGetInfo dlgkey", ltPtcGetKeyDlgPtr(hostptr), SM4_BLOCK_SIZE);
#endif
}
void chkHostHeartbeat(void *hostptr, void *subptr)
{
    int tmpflag = 0;
    ltCmdStcSnd_heartBeat tmpdata;
    strcpy(tmpdata.resvSnd, "hearthost");
    if (ltCmdFuncHostPack_heartBeat(ltPtcStcPtr(hostptr), parmNormIstc.addr485, 0, &tmpdata) < 0)
    {
        logerror("");
    }
    logHexBufStr("chkHostHeartbeat pack", ltPtcGetPackPtr(hostptr), ltPtcGetPackSizeV1(hostptr));
    tmpflag = chkSubParseRcv(subptr, ltPtcGetPackPtr(hostptr), ltPtcGetPackSizeV1(hostptr));
    logdebug("%d", tmpflag);
    if (tmpflag == 0)
    {
        logdebug("sub do wait:");
    }
    else if (tmpflag < 0)
    {
        logdebug("sub do error:");
    }
    tmpflag = chkHostParseRcv(hostptr, ltPtcGetPackPtr(subptr), ltPtcGetPackSizeV1(subptr));
    if (tmpflag == 0)
    {
        logdebug("host do wait:");
    }
    else if (tmpflag < 0)
    {
        logdebug("host do error:");
    }
}
static union
{
    uint16_t b;
    uint8_t a;
} mycheckUnion = {0x0102};

#define chkMaxSize 1
int main(int argsize, const char **args)
{
    char tmpPtcsnd[1024] = {};
    char tmpPtcRcv[1024] = {};
    if(0)
    {
        parmNormRead();
        parmNormPrint();
        parmNormIstc.addr485 = 0xa1;
        // memcpy(parmNormIstc.sn, "ABCDEFGHIJKLMNOP", sizeof(parmNormIstc.sn));
        parmNormIstc.loglev = 0x11;
#if PARM_NORMAL_CHK_FLAG
        parmNormIstc.chkparam = 99;
#endif
        parmNormPrint();
        parmNormSave();
        parmNormPrint();
        parmNormRead();
        parmNormPrint();
        // parmNormReset(PARAM_CFG_LEVEL_0);
        // parmNormPrint();
        // parmNormSave();
    }
    else
    {
        parmNormRead();
    }
    logdebug("union a = 0x%02x, b = 0x%04x", mycheckUnion.a, mycheckUnion.b);
    ltPtcInitV1(tmpPtcsnd, sizeof(tmpPtcsnd));
    ltPtcInitV1(tmpPtcRcv, sizeof(tmpPtcRcv));
    logdebug("remain bytes:%d", ltPtcGetDataMaxSizeV1(tmpPtcsnd));
    for (int i = 0; i < chkMaxSize; i++)
    {
        chkHostRwSn(tmpPtcsnd, tmpPtcRcv);
        chkHostRwAddr(tmpPtcsnd, tmpPtcRcv);
        chkHostGetInfo(tmpPtcsnd, tmpPtcRcv);
        chkHostHeartbeat(tmpPtcsnd, tmpPtcRcv);
    }
    usleep(100000);
    logdebug("end");
    return 0;
}